package com.travel.photos.ui.find;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.amlcurran.showcaseview.ShowcaseView;
import com.github.amlcurran.showcaseview.targets.ViewTarget;
import com.travel.R;
import com.travel.photos.FragmentAdapter;
import com.travel.photos.base.BaseActivity;
import com.travel.photos.ui.find.search.PhotoSearchUserFragment;
import com.travel.photos.ui.find.searchpurpose.PhotoSearchPurPoseFragment;
import com.travel.photos.ui.find.searchtravel.PhotoSearchTravelFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by meidou on 2016/10/22.
 */
public class PhotoSearchActivity extends BaseActivity {
    @Bind(R.id.photo_serach_tablayout)
    TabLayout mPhotoSerachTablayout;
    @Bind(R.id.photo_search_vp)
    ViewPager mPhotoSearchVp;
    @Bind(R.id.photo_find_serach_edit)
    EditText mEditText;
    @Bind(R.id.photo_serach_pic)
    ImageView mPhotoSerachPic;
    @Bind(R.id.photo_quxiao)
    TextView mPhotoQuxiao;
    private String searchcontent;
    private List<Fragment> mList = new ArrayList<>();
    private List<String> titlelist = new ArrayList<>();
    private  PhotoSearchUserFragment userfragment;
    private PhotoSearchTravelFragment travelfarment;
    private  PhotoSearchPurPoseFragment perposefragment;
    /* public String getSearchcontent() {
         Log.i("tag", "onEditorAct "+searchcontent);
             return   mEditText.getText().toString();
     }*/
    @Override
    public void initViews() {

    }

    @Override
    public void initData() {
        mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView view, int i, KeyEvent event) {
                if (i == EditorInfo.IME_ACTION_SEARCH) {
                    // 先隐藏键盘
                    ((InputMethodManager) mEditText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE))
                            .hideSoftInputFromWindow(
                                    PhotoSearchActivity.this.getCurrentFocus().getWindowToken(),
                                    InputMethodManager.HIDE_NOT_ALWAYS);
                    Log.i("tag", "onEditorAction: " + searchcontent);
                    PhotoSearchActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            searchcontent = mEditText.getText().toString();
                            initview(searchcontent);
                        }
                    });
                    return true;
                }
                return false;
            }
        });

    }

    public void initview(String i) {
        titlelist.clear();
        titlelist.add("用户");
        titlelist.add("轻游记");
        titlelist.add("目的地");
        userfragment = new PhotoSearchUserFragment(i);
        travelfarment = new PhotoSearchTravelFragment(i);
        perposefragment = new PhotoSearchPurPoseFragment(i);
        mList.clear();
        mList.add(userfragment);
        mList.add(travelfarment);
        mList.add(perposefragment);
        FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager(), mList, titlelist);
        mPhotoSearchVp.setAdapter(adapter);
        mPhotoSearchVp.setOffscreenPageLimit(0);
        mPhotoSerachTablayout.setupWithViewPager(mPhotoSearchVp);
        mPhotoSerachTablayout.setTabGravity(TabLayout.GRAVITY_CENTER); //tab居中显示
        //tab的字体选择器,默认黑色,选择时红色
        mPhotoSerachTablayout.setTabTextColors(Color.BLACK, Color.RED);
        //tab的下划线颜色,默认是红色
    }

    @Override
    public int getLayoutId() {
        return R.layout.photo_search;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        /*new ShowcaseView.Builder(this)
                .setTarget(new ViewTarget(mPhotoSerachPic))//set button as target
                .setContentTitle("不shi 按钮")
                .setContentText(" 不要点  ")
                .hideOnTouchOutside()
                .build();
*/
    }
    @OnClick(R.id.photo_quxiao)
    public void onClick() {
        this.finish();
    }
}
